package org.flik.server.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

@SuppressWarnings("serial")
@NamedQueries({
	@NamedQuery(name=TicketComment.QUERY_TICKET_COMMENTS, query = "select distinct tc from TicketComment tc inner join fetch tc.flikUser inner join fetch tc.ticket inner join fetch tc.replyTo left outer join fetch tc.replies where tc.ticket.id=:" + QueryParams.TICKET_ID + " order by tc.postedOn"), 
	@NamedQuery(name=TicketComment.QUERY_TICKET_COMMENT, query = "select distinct tc from TicketComment tc inner join fetch tc.flikUser where tc.id=:" + QueryParams.TICKET_COMMENT_ID)
	
})
@Entity
public class TicketComment implements Serializable {
	public static final String QUERY_TICKET_COMMENTS = "QUERY_TICKET_COMMENTS";
	public static final String QUERY_TICKET_COMMENT = "QUERY_TICKET_COMMENT";
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id; 

	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)	
	private FlikUser flikUser; 

	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)	
	private Ticket ticket; 
	
	private Date postedOn; 
	
	@Column(length=4000)
	private String comment; 

	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private TicketComment replyTo; 
	
	@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL, mappedBy="replyTo")
	private Set<TicketComment> replies; 
	
	
	public TicketComment() {
		postedOn = new Date();
	}
	
	public TicketComment(FlikUser flikUser, String comment, TicketComment replyTo, Ticket ticket) {
		this();
		this.flikUser = flikUser;
		this.postedOn = new Date();
		this.comment = comment;
		this.replyTo = replyTo;
		this.ticket = ticket;
	}

	public TicketComment(String comment) {
		this();
		this.comment = comment;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public FlikUser getFlikUser() {
		return flikUser;
	}

	public void setFlikUser(FlikUser flikUser) {
		this.flikUser = flikUser;
	}

	public Date getPostedOn() {
		return postedOn;
	}

	public void setPostedOn(Date postedOn) {
		this.postedOn = postedOn;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public TicketComment getReplyTo() {
		return replyTo;
	}

	public void setReplyTo(TicketComment replyTo) {
		this.replyTo = replyTo;
	}

	public Ticket getTicket() {
		return ticket;
	}

	public void setTicket(Ticket ticket) {
		this.ticket = ticket;
	}

	public Set<TicketComment> getReplies() {
		return replies;
	}

	public void setReplies(Set<TicketComment> replies) {
		this.replies = replies;
	}
	
	
}
